''' <summary>
''' This is a game component that implements IUpdateable.
''' </summary>
Public Class Background
    Inherits Microsoft.Xna.Framework.DrawableGameComponent
    Private myEffect As BasicEffect
    Private myEffectBack As BasicEffect
    Private myPrimitives As VertexPositionTexture()
    Private myPrimitivesback As VertexPositionTexture()
    Private colorTexture As Texture2D
    Private backTexture As Texture2D
    Private mRenderState As BlendState
    Private mintSize As Integer = 3000
    Private mintDist As Integer = 5000
    Private msngRotate As Single = 0
    Private mblnEnabled As Boolean = False

    Public Sub New(ByVal game As Game)
        MyBase.New(game)
        ' TODO: Construct any child components here
    End Sub

    ''' <summary>
    ''' Allows the game component to perform any initialization it needs to before starting
    ''' to run.  This is where it can query for any required services and load content.
    ''' </summary>
    Public Overrides Sub Initialize()
        ' TODO: Add your initialization code here
        MyBase.Initialize()
    End Sub

    Protected Overrides Sub LoadContent()
        ' Load the texture we want to display
        colorTexture = Game.Content.Load(Of Texture2D)("planet")
        backTexture = Game.Content.Load(Of Texture2D)("backing")
        ' Load custom effect and set parameters
        myEffect = New BasicEffect(Game.GraphicsDevice)
        myEffectBack = New BasicEffect(Game.GraphicsDevice)
        mRenderState = New BlendState
        With mRenderState
            .AlphaSourceBlend = Blend.One
            .ColorSourceBlend = Blend.One
            .AlphaDestinationBlend = Blend.One
            .ColorDestinationBlend = Blend.One
            .ColorBlendFunction = BlendFunction.Min
            .AlphaBlendFunction = BlendFunction.Min
        End With

        myEffect.TextureEnabled = True
        myEffectBack.TextureEnabled = True
        myEffect.Texture = colorTexture
        myEffectBack.Texture = backTexture

        myPrimitives = New VertexPositionTexture(3) {}
        myPrimitives(0) = New VertexPositionTexture()
        myPrimitives(0).Position = New Vector3(mintSize, -mintSize, mintDist)
        myPrimitives(0).TextureCoordinate = New Vector2(1, 0)
        myPrimitives(1) = New VertexPositionTexture()
        myPrimitives(1).Position = New Vector3(mintSize, mintSize, mintDist)
        myPrimitives(1).TextureCoordinate = New Vector2(1, 1)
        myPrimitives(2) = New VertexPositionTexture()
        myPrimitives(2).Position = New Vector3(-mintSize, -mintSize, mintDist)
        myPrimitives(2).TextureCoordinate = New Vector2(0, 0)
        myPrimitives(3) = New VertexPositionTexture()
        myPrimitives(3).Position = New Vector3(-mintSize, mintSize, mintDist)
        myPrimitives(3).TextureCoordinate = New Vector2(0, 1)


        Dim sngNewSize As Single = CSng(mintSize * 5.5)
        myPrimitivesback = New VertexPositionTexture(3) {}
        myPrimitivesback(0) = New VertexPositionTexture()
        myPrimitivesback(0).Position = New Vector3(sngNewSize, -sngNewSize, mintDist * 4 + 1)
        myPrimitivesback(0).TextureCoordinate = New Vector2(1, 0)
        myPrimitivesback(1) = New VertexPositionTexture()
        myPrimitivesback(1).Position = New Vector3(sngNewSize, sngNewSize, mintDist * 4 + 1)
        myPrimitivesback(1).TextureCoordinate = New Vector2(1, 1)
        myPrimitivesback(2) = New VertexPositionTexture()
        myPrimitivesback(2).Position = New Vector3(-sngNewSize, -sngNewSize, mintDist * 4 + 1)
        myPrimitivesback(2).TextureCoordinate = New Vector2(0, 0)
        myPrimitivesback(3) = New VertexPositionTexture()
        myPrimitivesback(3).Position = New Vector3(-sngNewSize, sngNewSize, mintDist * 4 + 1)
        myPrimitivesback(3).TextureCoordinate = New Vector2(0, 1)

        MyBase.LoadContent()

    End Sub

    Public Overrides Sub Draw(gameTime As Microsoft.Xna.Framework.GameTime)
        If (Me.Enabled) Then
            Dim State As RenderStates = New RenderStates
            RecordRenderState(State, GraphicsDevice)
            GraphicsDevice.BlendState = BlendState.AlphaBlend
            GraphicsDevice.DepthStencilState = DepthStencilState.None

            myEffectBack.CurrentTechnique.Passes(0).Apply()
            GraphicsDevice.DrawUserPrimitives(Of VertexPositionTexture)(PrimitiveType.TriangleStrip, myPrimitivesback, 0, 2)

            myEffect.CurrentTechnique.Passes(0).Apply()
            GraphicsDevice.DrawUserPrimitives(Of VertexPositionTexture)(PrimitiveType.TriangleStrip, myPrimitives, 0, 2)
            RestoreRenderState(State, GraphicsDevice)
        End If
        MyBase.Draw(gameTime)
    End Sub
    ''' <summary>
    ''' Allows the game component to update itself.
    ''' </summary>
    ''' <param name="gameTime">Provides a snapshot of timing values.</param>
    Public Overrides Sub Update(ByVal gameTime As GameTime)
        If (Me.Enabled) Then
            msngRotate += CSng(mintSize * 0.0000002)
            With CType(Game, Game1)
                myEffect.World = Matrix.Identity * Matrix.CreateTranslation(New Vector3(0, CSng(mintSize * 0.9), 0)) * Matrix.CreateRotationZ(msngRotate)
                myEffect.View = Matrix.CreateLookAt(Vector3.Zero, New Vector3(0, 0, mintDist + 1), Vector3.Up)
                myEffect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, Game.GraphicsDevice.Viewport.AspectRatio, 1, mintDist * 4 + 1)
                myEffectBack.World = Matrix.Identity * Matrix.CreateRotationZ(-msngRotate * 2 - 90)
                myEffectBack.View = Matrix.CreateLookAt(Vector3.Zero, New Vector3(0, 0, mintDist * 4 + 1), Vector3.Up)
                myEffectBack.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, Game.GraphicsDevice.Viewport.AspectRatio, 1, mintDist * 4 + 1)
            End With
        End If
        ' TODO: Add your update code here
        MyBase.Update(gameTime)
    End Sub

End Class
